﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace JBeamLibrary.VehicleParts.Elements
{
    public struct Color
    {
        #region Constants

        public static readonly Color Transparent = new Color(0, 0, 0, 0);
        public static readonly Color White = new Color(255, 255, 255, 255);
        public static readonly Color Black = new Color(255, 0, 0, 0);
        public static readonly Color Red = new Color(255, 255, 0, 0);
        public static readonly Color Green = new Color(255, 0, 255, 0);
        public static readonly Color Blue = new Color(255, 0, 0, 255);
        public static readonly Color Yellow = new Color(255, 255, 255, 0);
        public static readonly Color Magenta = new Color(255, 255, 0, 255);
        public static readonly Color Cyan = new Color(255, 0, 255, 255);

        #endregion

        #region Properties

        public byte A { get; set; }

        public byte R { get; set; }

        public byte G { get; set; }

        public byte B { get; set; }

        #endregion

        #region CTORs

        public Color(byte alpha, byte red, byte green, byte blue)
            : this()
        {
            A = alpha;
            R = red;
            G = green;
            B = blue;
        }

        #endregion

        #region Overrides

        public override string ToString()
        {
            return string.Format("[A={0}; R={1}; G={2}; B={3}]", A, R, G, B);
        }

        #endregion

        #region Methods

        public override bool Equals(object obj)
        {
            return
                obj is Color &&
                ((Color)obj).A.Equals(A) &&
                ((Color)obj).R.Equals(R) &&
                ((Color)obj).G.Equals(G) &&
                ((Color)obj).B.Equals(B);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static Color Parse(string str)
        {
            var match = Regex.Match(str, "\\[A=([0-9]+); R=([0-9]+); G=([0-9]+); B=([0-9]+)\\]");
            if (match.Success)
                return new Color(
                    byte.Parse(match.Groups[1].Value),
                    byte.Parse(match.Groups[2].Value),
                    byte.Parse(match.Groups[3].Value),
                    byte.Parse(match.Groups[4].Value));
            throw new FormatException(string.Format("The string '{0}' cannot be converted in to a valid Color object.", str));
        }

        public static bool TryParse(string str, out Color val)
        {
            try
            {
                val = Parse(str);
                return true;
            }
            catch (FormatException)
            {
                val = Black;
                return false;
            }
        }

        #endregion
    }
}
